package com.caiyouxi.boss.buser.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.caiyouxi.boss.buser.mapper.SysRightsMapper;
import com.caiyouxi.boss.buser.mapper.SysRolesMapper;
import com.caiyouxi.boss.buser.mapper.SysUserProfileMapper;
import com.caiyouxi.boss.buser.model.SysRightsBean;
import com.caiyouxi.boss.buser.model.SysRolesBean;
import com.caiyouxi.boss.buser.model.SysUserProfileBean;
import com.caiyouxi.boss.buser.service.SysRolesService;
import com.caiyouxi.common.bean.QueryParams;
import com.caiyouxi.common.bean.ResData;
import com.caiyouxi.common.cache.RedisService;
import com.caiyouxi.common.exception.ErrorCode;
import com.caiyouxi.common.service.BaseService;
import com.caiyouxi.common.util.ListDistinctUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.caiyouxi.common.util.ListDistinctUtils.get_rights_string;

/**
 * Created by Administrator on 2017/10/26.
 */
@Service
public class SysRolesServiceImpl extends BaseService implements SysRolesService{

    private Logger logger = Logger.getLogger(SysRolesServiceImpl.class);

    @Autowired
    private SysRolesMapper sysRolesMapper;
    @Autowired
    private SysRightsMapper sysRightsMapper;
    @Autowired
    private SysUserProfileMapper userProfileMapper;

    @Autowired
    RedisService redisService;

    //获取所有角色信息
    @Override
    public ResData get_all_roles(SysRolesBean bean) {

        Integer reids_page_size = Integer.parseInt( redisService.get("sys.pagesize").toString());
        if(null!=reids_page_size)
        {
            bean.setPage_size(reids_page_size);
        }
        //获取角色总数
        int total_count = sysRolesMapper.get_count(bean);
        //接收的List数据集合
        List<SysRolesBean> role_list = new ArrayList<>();
        //查出数据库的角色信息
        List<SysRolesBean> rolesBeanList = sysRolesMapper.get_all_roles(bean);
        for(SysRolesBean role : rolesBeanList){
            String right = role.getRights();
            String[] rights = get_rights_string(right);
            int right_count = rights.length;
            //拥有此角色用户的人数：现在是写死的：12
            int user_count = 12;
            role.setRight_count(right_count);
            role.setUser_count(user_count);
            JSONObject obj = (JSONObject) JSONObject.toJSON(role);
            obj.put("right_count",right_count);
            obj.put("user_count",user_count);
            //把角色信息重新加入到新的list中
            role_list.add(role);
        }
        QueryParams page_list = new QueryParams();
        page_list.setTotal(total_count);
        page_list.setPage(bean.getPage());
        page_list.setPage_size(bean.getPage_size());
        page_list.setList(role_list);
//        JSONArray jsonArray = new JSONArray();
//        jsonArray.add(page_list);
        return success(page_list);
    }

    //添加角色信息
    @Override
    public ResData add_roles(SysRolesBean bean) {

        logger.info("添加角色信息================");
        int role_exists = sysRolesMapper.roles_exists(bean);
        if(role_exists > 0){
            logger.info("角色信息已经存在");
            return failed(ErrorCode.SYS_ROLES_ADD_ERROR.getCode(),ErrorCode.SYS_ROLES_ADD_ERROR.getMsg(),null);
        }

        int add_result = sysRolesMapper.add_sys_role(bean);
        if(add_result == 1){
          return  success(null);
        }
        return failed(ErrorCode.SYS_ROLES_ADD_ERROR.getCode(),ErrorCode.SYS_ROLES_ADD_ERROR.getMsg(),null);
    }

    //修改角色信息
    @Override
    public ResData update_roles(SysRolesBean bean) {

        logger.info("修改角色信息");
        int role_exists = sysRolesMapper.update_role_exists(bean);
        if(role_exists>0){
            logger.info("不能修改本身以外的角色信息");
            return failed(ErrorCode.SYS_ROLES_UPDATE_ERROR.getCode(),ErrorCode.SYS_ROLES_UPDATE_ERROR.getMsg(),null);
        }
        int update_result = sysRolesMapper.update_sys_role(bean);
        if(update_result == 1){
            return success(null);
        }
        return failed(ErrorCode.SYS_ROLES_UPDATE_ERROR.getCode(),ErrorCode.SYS_ROLES_UPDATE_ERROR.getMsg(),null);
    }

    //获取角色信息
    @Override
    public ResData get_role_by_id(SysRolesBean bean) {
        logger.info("通过id获取单个角色信息");
        SysRolesBean srb_bean = sysRolesMapper.get_role_by_id(bean);
        String right = srb_bean.getRights();
        String[] rights = get_rights_string(right);
        int right_count = rights.length;
        srb_bean.setRight_count(right_count);
        return success(srb_bean);
    }


    //删除角色信息
    @Override
    public ResData delete_roles(SysRolesBean bean) {
        logger.info("删除角色信息");
        int delete_result = sysRolesMapper.delete_sys_role(bean);
        if(delete_result == 1){
            return success(null);
        }
        return failed(ErrorCode.SYS_ROLES_DELETE_ERROR.getCode(),ErrorCode.SYS_ROLES_DELETE_ERROR.getMsg(),null);
    }

    //权限列表
    @Override
    public JSONArray get_rights_list() {
        //获取所有权限
        List<Integer> rights_id = sysRightsMapper.get_rights_id();

        List<SysRightsBean> one_list = sysRightsMapper.get_rights_list(rights_id);

        //一级目录
        JSONArray array_one = new JSONArray();
        for (SysRightsBean srb : one_list) {
            JSONObject obj = (JSONObject) JSONObject.toJSON(srb);
            List<SysRightsBean> two_list = sysRightsMapper.get_rights_evel(srb.getId(), rights_id);
            //二级目录
            JSONArray array_two = new JSONArray();
            for (SysRightsBean two_srb : two_list) {
                JSONObject two_obj = (JSONObject) JSONObject.toJSON(two_srb);
                List<SysRightsBean> three_list = sysRightsMapper.get_rights_evel(two_srb.getId(), rights_id);
                //三级目录
                JSONArray array_three = (JSONArray) JSONArray.toJSON(three_list);
                two_obj.put("list", array_three);
                array_two.add(two_obj);
            }
            obj.put("list", array_two);
            array_one.add(obj);

        }
        return array_one;
    }

    //获取所有权限
    @Override
    public ResData get_all_rights(SysRolesBean role_bean) {

        JSONArray array_one = get_rights_list();
        //获取角色权限
        List<SysRolesBean> role_rights = sysRolesMapper.get_all_role_rights(role_bean);
        JSONArray array_role_rights = new JSONArray();
        for(SysRolesBean role : role_rights){
            String right = role.getRights();
            String[] rights = ListDistinctUtils.get_rights_string(right);
            List<String>  right_list = Arrays.asList(rights);
            array_role_rights = (JSONArray) JSONArray.toJSON(right_list);
        }
        //总的对象
        JSONObject right_json = new JSONObject();
        right_json.put("all-rights",array_one);
        right_json.put("roles_rights",array_role_rights);

        return success(right_json);

    }

    //跟新用户权限
    @Override
    public ResData update_roles_rights(SysRolesBean bean) {

        int result = sysRolesMapper.update_role_rights(bean);
        if(result == 1){
           return success(null);
        }
        return failed(ErrorCode.SYS_ROLES_UPDATE_ERROR.getCode(),ErrorCode.SYS_ROLES_UPDATE_ERROR.getMsg(),null);
    }

    //通过角色id获取此角色的所有用户
    @Override
    public ResData get_user_role_id(SysRolesBean bean) {
        List<SysUserProfileBean> user_list = sysRolesMapper.get_user_role_id(bean);

        return success(user_list);
    }

    //给用户添加新的角色

    @Override
    public ResData update_role_to_user( SysRolesBean srb) {
        SysUserProfileBean bean = new SysUserProfileBean();
        bean.setId(srb.getUser_id());
        SysUserProfileBean user_roles = userProfileMapper.get_sys_role_list(bean);
        String roles = user_roles.getRoles();
        //判断该用户是否有此角色
        if(roles.indexOf(srb.getId().toString())>0){
            logger.info("该用户拥有此权限");
            return failed(ErrorCode.SYS_ROLES_UPDATE_ERROR.getCode(),ErrorCode.SYS_ROLES_UPDATE_ERROR.getMsg(),null);
        }else{
            String new_roles = roles+srb.getId().toString()+"|";
            bean.setRoles(new_roles);
            int result = sysRolesMapper.update_role_to_user(bean);
            if(result>0){
                return success(null);
            }
        }
        return failed(ErrorCode.SYS_ROLES_UPDATE_ERROR.getCode(),ErrorCode.SYS_ROLES_UPDATE_ERROR.getMsg(),null);
    }

}
